//
//  KYNetTool.m
//  KYKJPackDemo
//
//  Created by Jordan zhou on 2017/9/28.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "KYNetTool.h"

#define kTimeOutInterval 10

@interface KYNetTool()

@property (nonatomic, assign, getter = isConnectNetWork) BOOL connectNet;

@end

@implementation KYNetTool

/**
 *  返回请求单例对象
 */
+ (instancetype)shareInstance
{
    static KYNetTool *httpTool;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (httpTool == nil) {
            httpTool = [[KYNetTool alloc] init];
        }
    });
    return httpTool;
}
///**
// *  返回当前网络状态
// */
//- (BOOL)connectNetWork
//{
//    Reachability *networkReachability = [Reachability reachabilityForInternetConnection];
//    NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];
//    if (networkStatus == NotReachable) {
//        NSLog(@"There IS NO internet connection");
//        self.isOpenNetWork = NO;
//    } else {
//        NSLog(@"There IS internet connection");
//        self.isOpenNetWork = YES;
//    }
//    
//    return self.isOpenNetWork;
//}

/**
 *  创建请求manager
 */
+ (AFHTTPSessionManager *)manager
{
    // https 请求设置
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    [securityPolicy setValidatesDomainName:NO];
    [securityPolicy setAllowInvalidCertificates:YES];
    
    // 创建请求单例
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // https
    manager.securityPolicy = securityPolicy;
    
    // 超时时间
    manager.requestSerializer.timeoutInterval = kTimeOutInterval;
    
    // 声明上传的是json格式的参数，需要你和后台约定好，不然会出现后台无法获取到你上传的参数问题
    //    manager.requestSerializer = [AFHTTPRequestSerializer serializer]; // 上传普通格式
    manager.requestSerializer = [AFJSONRequestSerializer serializer]; // 上传JSON格式
    
    // 根据不同场景传入token
#if Service_API == 0 || Service_API == 2
//    [manager.requestSerializer setValue:[HTUserData shareInstance].token forHTTPHeaderField:@"Authorization"];
    //aigongzuo \ 其它
#else
//    [manager.requestSerializer setValue:[HTUserData shareInstance].token forHTTPHeaderField:@"token"];
    
#endif
    
    // 声明获取到的数据格式
    //    manager.responseSerializer = [AFHTTPResponseSerializer serializer]; // AFN不会解析,数据是data，需要自己解析
    //    manager.responseSerializer = [AFJSONResponseSerializer serializer]; // AFN会JSON解析返回的数据
    //    个人建议还是自己解析的比较好，有时接口返回的数据不合格会报3840错误，大致是AFN无法解析返回来的数据
    
    return manager;
}

+ (void)postWithURL:(NSString *)url params:(NSDictionary *)params success:(void (^)(id json))success failure:(void (^)(NSError *error))failure
{
    // 1.创建请求管理对象
    AFHTTPSessionManager *mgr = [self manager];
    
    // 2.发送请求
    [mgr POST:url parameters:params
      success:^(NSURLSessionDataTask *operation, id responseObject) {
          if (success) {
              success(responseObject);
          }
      } failure:^(NSURLSessionDataTask *operation, NSError *error) {
          if (failure) {
              failure(error);
          }
      }];
}

+ (void)getWithURL:(NSString *)url params:(NSDictionary *)params success:(void (^)(id json))success failure:(void (^)(NSError *error))failure
{
    // 1.创建请求管理对象
    AFHTTPSessionManager *mgr = [self manager];
    
    // 2.发送请求
    [mgr GET:url parameters:params success:^(NSURLSessionDataTask *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

@end
